<template>
	<view class="content">
		<custom-waterfalls-flow :value="lists" ref="waterfallsFlowRef" imageKey="coverImage" @wapperClick="linkUrl"
			@imageClick="linkUrl">
			<template v-slot:default="item">
				<view class="item">
					<view class="title">{{item.title || ''}}</view>
					<view class="desc">{{item.describe || ''}}</view>
					<view class="_user">
						<view>
							<u--image :src="item.userinfo.avatar" shape="circle" width="20px" height="20px"
								:showLoading="true"></u--image>
							<text>{{item.userinfo.username || ''}}</text>
						</view>
						<view>
							<u-icon name="heart" color="#888" size="22"></u-icon>
							<text>0</text>
						</view>
					</view>
				</view>
			</template>
		</custom-waterfalls-flow>
		<u-loadmore :status="status" />
	</view>
</template>

<script>
	import {
		getDynamicsLists
	} from '../../../api/user.js';
	export default {
		data() {
			return {
				req: {
					page: 1,
					pageSize: 10,
					status: 'approved'
				},
				lists: [],
				status: 'loadmore',
			}
		},
		onShow() {
			this.$nextTick(()=>{
				this.$refs.waterfallsFlowRef.refresh();
			});
		},
		onLoad() {
			this.getDynamicsLists();
		},
		//
		// 上拉加载数据
		onReachBottom() {
			if (this.status == 'nomore') return;
			this.status = 'loading';
			this.req.page = ++this.req.page;
			// 获取列表数据
			this.getDynamicsLists();
		},
		// 下拉刷新数据
		onPullDownRefresh() {
			// 清空数据
			this.req.page = 1;
			this.status = 'loadmore';
			this.lists = [];
			this.getDynamicsLists();
			uni.stopPullDownRefresh();
			uni.showToast({
				mask: true,
				title: "刷新成功",
				icon: "none"
			});
		},
		methods: {
			/** 
			@method 获取动态列表
			**/
			async getDynamicsLists() {
				try {
					const res = await getDynamicsLists(this.req);
					if (res.code == 200) {
						this.lists.push(...res.data.data);
						// 判断数据数量，修改状态
						if (res.data.data.length == 0 || res.data.data.length < this.req.pageSize) {
							this.status = 'nomore';
							return;
						}
					}
				} catch (err) {
					console.log(err);
				}
			},
			linkUrl(e) {
				uni.navigateTo({
					url: `/pages/index/details?id=${e._id}`
				});
			}
		}
	}
</script>

<style>
	page {
		background: #f2f2f2;
	}
</style>
<style scoped lang="scss">
	.content {
		width: auto;
		padding: 20rpx;

		.item {
			padding: 10rpx 10rpx 10rpx 10rpx;

			._user {
				width: auto;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding-top: 10rpx;

				view {
					display: flex;
					align-items: center;

					text {
						font-size: 24rpx;
						margin-left: 10rpx;
					}
				}
			}

			.title {
				color: #333;
				font-size: 30rpx;
				font-weight: 500;
				white-space: nowrap;
				/*文本强制一行显示*/
				overflow: hidden;
				/*文本溢出隐藏*/
				text-overflow: ellipsis;
				/*溢出的文本显示省略号*/
			}

			.desc {
				color: #888;
				font-size: 28rpx;
				display: -webkit-box;
				/* 必须设置，用于多行溢出 */
				-webkit-box-orient: vertical;
				/* 设置盒子排列方向为垂直 */
				-webkit-line-clamp: 2;
				/* 限制显示的行数，例如显示 3 行 */
				overflow: hidden;
				/* 隐藏超出的内容 */
			}
		}
	}
</style>